<template>
  <div class="login-page">
    <div class="back">
      <!-- 事件修饰符native 绑定组件的原生事件,属于组件根元素 -->
      <!-- $router.back() 返回上一次访问路由 $router.forward() $router.to()  -->
      <geek-icon name="esay-close" @click.native="$router.back()"></geek-icon>
    </div>
    <h3 class="title">短信登录</h3>
    <!-- 表单 -->
    <van-form ref="form">
      <van-field name="mobile" v-model="form.mobile" :rules="rules.mobile" placeholder="请输入手机号"></van-field>
      <van-field v-model="form.code" :rules="rules.code" placeholder="请输入验证码" @click="sendMessage()">
        <template #button>
          <span class="send">{{secound>0?`${secound}秒之后再发送`:"发送验证码"}}</span>
        </template>
      </van-field>
    </van-form>
      <van-button @click="login()">登录</van-button>
  </div>
</template>
<script>
import { userLogin, sendMessage } from '@/api/user.js'
export default {
  name: 'LoginPage',
  data () {
    return {
      form: {
        mobile: '13011836256',
        code: '246810'
      },
      rules: {
        mobile: [{ required: true, message: '请输入手机号' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确' }],
        code: [{ required: true, message: '请输入验证码' },
          { pattern: /\d{6}$/, message: '验证码为6位字符' }]
      },
      secound: 0,
      timer: null
    }
  },
  methods: {
    async login () {
      // 表单整体校验后
      await this.$refs.form.validate()
      // 发送登陆请求
      const [err, res] = await userLogin(this.form)
      // 错误情况
      if (err) return this.$toast.fail('登陆失败')
      // 登陆成功后存储token
      this.$store.commit('user/setToken', res.data.data.token)
      // 存储refresh_token
      this.$store.commit('user/setRefreshToken', res.data.data.refresh_token)
      // 跳转到原先的页面或者跳转到首页
      this.$router.push(this.$route.query.returnUrl || '/')
    },
    async sendMessage () {
      // 准备 发送验证码 按钮
      // 编写发送短信API接口
      // 点击 发送验证码 按钮，
      // 判断是否已经发送，
      // 校验手机号，
      // 调用发送短信API，
      // 成功后开启60秒倒计时，不可再次发送
      if (this.secound > 0) return
      await this.$refs.form.validate('mobile')
      const [err] = await sendMessage(this.form.mobile)
      // 如果有错误
      if (err) return this.$toast.fail('发送失败')
      // 没有错误开启定时器倒计时
      this.secound = 60
      clearInterval(this.timer)
      setInterval(() => {
        this.secound--
        if (this.secound <= 0) {
          return clearInterval(this.timer)
        }
      }, 1000)
    }
  },
  // 销毁组件前，删除定时器
  beforeDestory () {
    clearInterval(this.timer)
  }
}
</script>
<style scoped lang="less">
.login-page {
  padding: 0 32px;
  .back {
    height: 60px;
    display: flex;
    align-items: center;
    .geek-icon {
      font-size: 22px;
      color: #ccc;
      position: relative;
      left: -15px;
    }
  }
  .title {
    font-size: 22px;
    line-height: 1;
    padding: 30px 0;
  }
  .van-cell {
    padding: 20px 0;
    &::after{
      left: 0;
      right: 0;
    }
  }
  .van-button {
    width: 100%;
    margin-top: 40px;
    height: 50px;
    color: #fff;
    font-size: 16px;
    border: none;
    background: linear-gradient(to right,#FF9999,#FFA179);
    // 按钮激活可用
    &.active {
      background: linear-gradient(to right,#FE4F4F,#FC6627);
    }
  }
    .send {
    font-size: 12px;
    color: #A5A6AB;
  }
}
</style>
